/*
* 首页模块
* */
import api from '../../api/index'
import ajax from '../../utils/ajax'
import drawTrade from '../../utils/trade' // 城市交易图
import count from '../../utils/count' // 数字动态计数器
import cardItem from '../../utils/cardItem' // 领卡数据图
import deviceLife from '../../utils/deviceLife' // 设备运行状态图
import userTotal from '../../utils/userTotal' // 用户分析图
import map from '../../utils/map' // 地图数据图
import numberFormat from '../../utils/numberFormat' // 给数据加逗号
import { Message } from 'element-ui'

const TOP_RIGHT_TITLE = '领卡趋势图'
const BOTTOM_LEFT_TITLE = '设备使用情况'
const BOTTOM_RIGHT_TITLE = '用户层分析'
const userTotalColor = ['#1B72FD', '#5196FF', '#1BC5FD', '#50E3C2', '#ACFCF5'] // 用户明细个个颜色值

const indexItem = {
  namespaced: true,
  state: {
    leftTopLoading: false, // 左上loading
    leftMiddleLoading: false, // 左中loading
    leftBottomLoading: false, // 左下loading
    middleTopLoading: false, // 中上loading
    rightTopLoading: false, // 右上loading
    allTrade: '',
    allCard: '',
    userTotalArr: '', // 用户统计右侧
    userTotalAllCount: 0, // 用户总数
    userTotalColor: '', // 用户明细个个颜色值
    totalNavList: [
      {title: '实时', id: 1, isActive: true},
      {title: '昨日', id: 4, isActive: false},
      {title: '7天前', id: 2, isActive: false},
      {title: '总量', id: 3, isActive: false}
    ],
    cardItemList: [
      {title: '24h', id: 1, isActive: true},
      {title: '近3天', id: 2, isActive: false},
      {title: '近7天', id: 3, isActive: false}
    ],
    deviceLifeList: [
      {id: 'allDevice', name: '总设备数量'},
      {id: 'onLineDevice', name: '在线设备数'},
      {id: 'tradeDevice', name: '交易设备数'}
    ]
  },
  mutations: {
    leftTopLoading (state, data) { // 左上loading
      state.leftTopLoading = data
    },
    leftMiddleLoading (state, data) { // 左中loading
      state.leftMiddleLoading = data
    },
    leftBottomLoading (state, data) { // 左下loading
      state.leftBottomLoading = data
    },
    middleTopLoading (state, data) { // 中上loading
      state.middleTopLoading = data
    },
    rightTopLoading (state, data) { // 右上loading
      state.rightTopLoading = data
    },
    allTrade (state, data) { // 总交易
      state.allTrade = data
    },
    allCard (state, data) { // 储存总领卡
      state.allCard = data
    },
    userTotalArr (state, data) {
      state.userTotalArr = data
    },
    userTotalAllCount (state, data) {
      state.userTotalAllCount = data
    },
    userTotalColor (state, data) {
      state.userTotalColor = data
    }
  },
  actions: {
    getAllTrade ({commit}, obj) { // 获取全部城市的交易量
      commit('leftTopLoading', true)
      ajax.get(api.indexItem.allTrade, {cityCode: obj.cityCode}).then((res) => {
        commit('leftTopLoading', false)
        if (res.msg && res.msg.code === '20000') {
          let x = []
          let y = []
          let ALL_TRADE_TITLE = 'TOP 20 城市交易量'
          let list = res.data
          if (obj.cityCode) {
            ALL_TRADE_TITLE = 'TOP 20 线路交易量'
            for (let i = 0; i < list.list.length; i++) {
              x.push(list.list[i].lineName)
              y.push(list.list[i].payNumber)
            }
          } else {
            for (let i = 0; i < list.cityTradeTotalList.length; i++) {
              x.push(list.cityTradeTotalList[i].city)
              y.push(list.cityTradeTotalList[i].cityTradeTotal)
            }
          }
          drawTrade(ALL_TRADE_TITLE, x, y)
        } else {
          Message({showClose: true, message: '数据加载错误', type: 'error'})
        }
      })
    },
    getLineTrade ({commit}) { // 获取每个城市线路交易量
    },
    getTotal ({commit}, obj) { // 获取领卡数量 交易数量
      commit('middleTopLoading', true)
      ajax.get(api.indexItem.allTotal, {cityCode: obj.cityCode, dateType: obj.id - 1}).then((res) => {
        commit('middleTopLoading', false)
        if (res.msg && res.msg.code === '20000') {
          commit('allCard', numberFormat(res.data[0].allCard))
          commit('allTrade', numberFormat(res.data[0].allTrade))
        } else {
          Message({showClose: true, message: '数据加载错误', type: 'error'})
        }
      })
    },
    getCardItem ({commit}, obj) { // 领卡细化图表
      commit('leftBottomLoading', true)
      ajax.get(api.indexItem.cardItem, {cityCode: obj.cityCode}).then((res) => {
        commit('leftBottomLoading', false)
        if (res.msg && res.msg.code === '20000') {
          cardItem(TOP_RIGHT_TITLE, res.data[obj.id].X, res.data[obj.id].Y)
        } else {
          Message({showClose: true, message: '数据加载错误', type: 'error'})
        }
      })
    },
    getDeviceLife ({commit}, obj) { // 设备情况
      commit('leftMiddleLoading', true)
      ajax.get(api.indexItem.deviceLife, {cityCode: obj.cityCode}).then((res) => {
        commit('leftMiddleLoading', false)
        if (res.msg && res.msg.code === '20000') {
          let deviceLifeData = [
            {
              name: '离线设备',
              value: res.data.deviceLife.offLineDevice
            },
            {
              name: '在线设备',
              value: res.data.deviceLife.onLineDevice
            }
          ]
          let deviceTotal = res.data.deviceTotal
          deviceLife(BOTTOM_LEFT_TITLE, deviceLifeData)
          // 遍历返回对象加入计数器
          for (let objName in deviceTotal) {
            count(objName, deviceTotal[objName], 2.5)
          }
        } else {
          Message({showClose: true, message: '数据加载错误', type: 'error'})
        }
      })
    },
    getUserTotal ({commit}, obj) { // 用户统计
      commit('rightTopLoading', true)
      ajax.get(api.indexItem.userTotal, {cityCode: obj.cityCode}).then((res) => {
        commit('rightTopLoading', false)
        if (res.msg && res.msg.code === '20000') {
          let userTotalData = [
            {
              name: '活跃用户',
              value: res.data.totalUser.aliveUser
            },
            {
              name: '普通用户',
              value: res.data.totalUser.normalUser
            },
            {
              name: '沉睡用户',
              value: res.data.totalUser.sleepUser
            },
            {
              name: '流失用户',
              value: res.data.totalUser.loseUser
            },
            {
              name: '回流用户',
              value: res.data.totalUser.backUser
            }
          ]
          let allCount = res.data.totalUser.userCount
          userTotal(BOTTOM_RIGHT_TITLE, userTotalData, allCount, userTotalColor)
          commit('userTotalArr', userTotalData)
          commit('userTotalAllCount', allCount)
          commit('userTotalColor', userTotalColor)
        } else {
          Message({showClose: true, message: '数据加载错误', type: 'error'})
        }
      })
    },
    setMap ({commit}, obj) { // 写入地图
      map(setMapData(obj.data).newData)
    }
  }
}

const setMapData = (data) => {
  let newData = []
  let colorData = []
  for (let i = 0; i < data.length; i++) {
    let modulePath = data[i].modulePath
    if (modulePath !== '000000') {
      let name = data[i].moduleName
      let color = data[i].color
      let value = data[i].value
      newData.push({
        name: name, value: value
      })
      colorData.push(color)
    }
  }
  return {
    newData: newData
  }
}

export default indexItem
